package com.tju.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tju.eduservice.entity.EduChapter;
import com.tju.eduservice.entity.EduVideo;
import com.tju.eduservice.entity.chapter.ChapterVo;
import com.tju.eduservice.entity.chapter.VideoVo;
import com.tju.eduservice.mapper.EduChapterMapper;
import com.tju.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.eduservice.service.EduVideoService;
import com.tju.serviceHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author gsc
 * @since 2021-11-29
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterById(String courseId) {
        //查询一级章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper);

        //根据课程id查询所有的小节
        QueryWrapper<EduVideo> wrapper1 = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapper1);
        //封装一级章节和二级小节
        System.out.println("小节信息" + eduVideoList);
        List<ChapterVo> finalList = new ArrayList<>();
        for (EduChapter eduChapter : eduChapters) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);
            for (EduVideo eduVideo : eduVideoList) {
                if(eduVideo.getChapterId().equals(chapterVo.getId())){
                    VideoVo videoVo = new VideoVo();
                    videoVo.setId(eduVideo.getId());
                    videoVo.setTitle(eduVideo.getTitle());
                    videoVo.setVideoSourceId(eduVideo.getVideoSourceId());
                    chapterVo.getChildren().add(videoVo);
                }
            }
        }
        return finalList;


    }
    //删除章节  如果章节下面有小节，无法删除，没有章节才能删除
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapter id来查询小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        if(count == 0){
            int i = baseMapper.deleteById(chapterId);
            return (i > 0);
        }else {
            throw new GuliException(20001,"当前章节内小节不为空，删除失败");

        }


    }

    @Override
    public void removeChapterByCourseId(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);
    }
}
